package com.mz.ozy.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mz.ozy.R;
import com.mz.ozy.activity.me.MsgActivity;
import com.mz.ozy.activity.me.MsgDetailActivity;
import com.mz.ozy.activity.me.OrderLogisticsInfoActivity;
import com.mz.ozy.bean.MyOrderBean;
import com.mz.ozy.bean.MyOrderInfoBean;
import com.mz.ozy.bean.MyOrderInfoBean1;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.utils.StringUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.sobot.chat.ZCSobotApi;
import com.sobot.chat.api.model.Information;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/11/22.
 */

public class LogisticsListAdapter extends BaseQuickAdapter<MyOrderInfoBean1.OrdermapBean.ProdcutListBean, BaseViewHolder> {

    private String orderId;
    private String mAddress;
    public LogisticsListAdapter(Context context, @Nullable List<MyOrderInfoBean1.OrdermapBean.ProdcutListBean> data, String orderId,String address) {
        super(R.layout.view_item_logistics_obligation, data);
        this.orderId = orderId;
        mContext = context;
        mAddress = address;
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderInfoBean1.OrdermapBean.ProdcutListBean item) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        boolean isSingle = false;
        LogisticsAdapter myAdapter;
        if (item.getListsEntities() != null && !item.getListsEntities().isEmpty()) {
            //只显示4张图片
//            if (item.getListsEntities().size() > 4) {
//                ArrayList<MyOrderInfoBean1.OrdermapBean.ProdcutListBean.ListsEntitiesBean> goodsList = new ArrayList<>();
//                for (int i = 0; i < 4; i++) {
//                    goodsList.add(item.getListsEntities().get(i));
//                }
//                myAdapter = new LogisticsAdapter(goodsList);
//            } else {
                myAdapter = new LogisticsAdapter(item.getListsEntities());
//            }
            if (item.getListsEntities().size() == 1) {
                isSingle = true;
            } else {
                isSingle = false;
            }
        } else {
            myAdapter = new LogisticsAdapter(null);
            isSingle = false;
        }
        RecyclerView rvIcons = helper.getView(R.id.rv_icons);
        final RelativeLayout rlRvParent = helper.getView(R.id.rl_rv_parent);
        LinearLayout llSingleGoods = helper.getView(R.id.ll_single_goods);
        ImageView ivIcon = helper.getView(R.id.iv_icon);
        rvIcons.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return rlRvParent.onTouchEvent(event);
            }
        });
        if (isSingle) {
            rvIcons.setVisibility(View.GONE);
            llSingleGoods.setVisibility(View.VISIBLE);
            ivIcon.setVisibility(View.VISIBLE);
            final MyOrderInfoBean1.OrdermapBean.ProdcutListBean.ListsEntitiesBean prodcutListBean = item.getListsEntities().get(0);
            String img_zt = "";
            if (!TextUtils.isEmpty(prodcutListBean.getImg_zt())) {
                final String[] splitImgZt = prodcutListBean.getImg_zt().split(",");
                if (splitImgZt.length > 0) {
                    img_zt = splitImgZt[0];
                }
            }
            GlideUtils.loadImg(mContext, ivIcon, img_zt, R.mipmap.default_foods_icon);
            helper.setText(R.id.tv_title, prodcutListBean.getProduct_name())
                    .setText(R.id.tv_info, "null".equals(prodcutListBean.getArrribute_name()) ? "" : prodcutListBean.getArrribute_name())
                    .setText(R.id.tv_count, "×" + prodcutListBean.getNum());
        } else {
            rvIcons.setVisibility(View.VISIBLE);
            ivIcon.setVisibility(View.GONE);
            llSingleGoods.setVisibility(View.GONE);
            myAdapter.setHasStableIds(true);
            rvIcons.setLayoutManager(linearLayoutManager);
            rvIcons.setAdapter(myAdapter);
        }

        if (helper.getAdapterPosition() == 0) {
            helper.setVisible(R.id.view_divider_top, true);
        } else {
            helper.setVisible(R.id.view_divider_top, false);
        }

        LinearLayout llBtn = helper.getView(R.id.ll_btn);

        TextView tvOrderState = helper.getView(R.id.tv_order_state);
        //TextView tvTime = helper.getView(R.id.tv_time);

        TextView tvOrderId = helper.getView(R.id.tv_orderId);
        tvOrderId.setText("订单编号："+orderId);
        TextView tvNumber = helper.getView(R.id.tv_number);
        if(!TextUtils.isEmpty(item.getNumber()))
            tvNumber.setText("快递单号："+item.getNumber());
        else
            tvNumber.setText("快递单号：");

//        TextView tvChange = helper.getView(R.id.tv_change);
//        TextView tvCoupon = helper.getView(R.id.tv_coupon);
//        TextView tvXFjf = helper.getView(R.id.tv_xf_jf);

        llBtn.setVisibility(View.VISIBLE);
        helper.setVisible(R.id.view_order_bottom, true);
//        llChange.setVisibility(View.VISIBLE);

        //tvOrderId.setText(item.getId());
        //String yhPrice = "";

        //tvGoodsCount.setText("总价¥" + getPrice(item) + ",优惠¥" + yhPrice + "  实付款");
        //现金总额
//        tvMoney.setText("¥" + item.getPrice());

//        if (item.getType() == 1) {
//            tvGoodsCount.setText("实付款");
//            tvMoney.setText(item.getActual_price() + "欧花");
//
//        } else {
//            tvGoodsCount.setText("总价¥" + getPrice(item) + ",优惠¥" + yhPrice + "  实付款");
//            tvMoney.setText("¥" + item.getActual_price());
//
//        }

//        tvFreight.setText("(运费¥" + StringUtils.formattingDecimal(item.getLogicost(), 2) + ")");

//        TextView btnCancel = helper.getView(R.id.btn_cancel);
//        TextView btnPay = helper.getView(R.id.btn_pay);
//        TextView tvPayTime = helper.getView(R.id.tv_pay_time);
//        TextView btnConfirm = helper.getView(R.id.btn_confirm);
//        TextView btnPinl = helper.getView(R.id.btn_pinl);


//        TextView btnEvaluate = helper.getView(R.id.btn_evaluate);
//        TextView btn_rerurn = helper.getView(R.id.btn_rerurn);

        helper.addOnClickListener(R.id.btn_logistics);
        TextView btnLogistics = helper.getView(R.id.btn_logistics);
        btnLogistics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //OrderLogisticsInfoActivity.start(mContext, orderId, 0);
                if(!TextUtils.isEmpty(item.getNumber())) {
                    Intent intent = new Intent(mContext, OrderLogisticsInfoActivity.class);
                    intent.putExtra("code", item.getCode());
                    intent.putExtra("number", item.getNumber());
                    intent.putExtra("order", orderId);
                    intent.putExtra("beanType", 0);
                    intent.putExtra("state", item.getState());
                    intent.putExtra("address", mAddress);
                    mContext.startActivity(intent);
                }
            }
        });

        //btnLogistics.setVisibility(View.GONE);

        //tvTime.setVisibility(View.GONE);
        //  订单状态；0-订单生成；1-已发货；2-已收货；3-退货中；4-已退货；5-已取消 6-未发货
        //  支付状态 0-生成，1-支付中，2-支付成功

        // 待付款：订单状态为0  支付状态为0
        // 待发货：订单状态为6 支付状态为2
        // 待收货：订单状态为1  支付状态为2

        tvOrderState.setText("未知");
        switch (item.getState()) {
            case 0://订单生成
                tvOrderState.setText("待支付");
                break;
            case 1: //已发货
                tvOrderState.setText("待发货");

                break;
            case 2://已收货
                tvOrderState.setText("待收货");
                break;
            case 3://退货中
                tvOrderState.setText("待退款");
                break;
            case 4://已退货
                tvOrderState.setText("已退款");
                break;
            case 5://已取消
                tvOrderState.setText("已完成");
                break;
            case 6://未发货
                tvOrderState.setText("已取消");
                llBtn.setVisibility(View.GONE);
                helper.setGone(R.id.view_order_bottom, false);
                break;
            case 7://订单超时
                tvOrderState.setText("退款失败");
                llBtn.setVisibility(View.GONE);
                helper.setGone(R.id.view_order_bottom, false);
                break;
        }
    }

}
